Random Walk Programs

The One-Dimensional Random Walk

steps[n_] := Table[(-1)^Random[Integer], {n}]

steps[10]

FoldList[Plus, 0, %]

Walk1D[n_] :=  FoldList[Plus, 0, Table[(-1)^Random[Integer], {n}]]

Walk1D[10]

The Two-Dimensional Lattice Walk

Walk2D[n_] := FoldList[Plus, {0, 0}, 
{{0,1},{1,0},{0,-1},{-1,0}}[[Table[Random[Integer, {1, 4}], {n}]]]]

Walk2D[20]

Mean Shape of a Two-Dimensional Lattice Walk

Apply[Plus, {xf, yf}^2]

SquareDistance[walk_List] := Apply[Plus, Last[walk]^2]

Walk2D[10]

SquareDistance[ Walk2D[10] ]

SquareDistance[ Walk2D[10] ]

MeanSquareDistance[n_Integer, m_Integer] := 
	Module[{walk2D},
	walk2D[s_] := FoldList[Plus, {0, 0}, 
{{0,1},{1,0},{0,-1},{-1,0}}[[Table[Random[Integer,{1,4}],{s}]]]]; 
	N[Sum[Apply[Plus, Last[walk2D[n]]^2], {m}]/m]
	]

\(AverageDistance[n_Integer, \ m_Integer]\  := \ 
	
      Module[{w2D, w, t}, 
	
        w2D[s_] := 
          FoldList[Plus, \ {0, 0}, \ 

            \ \ \ \ \ \ \ \ \ \ \ {{0, 1}, {1, 0}, {0, \(-1\)}, {\(-1\), 0}}

              			\ \ \ \ \ \ \ [
              \([Table[Random[Integer, {1, 4}], {s}]]\)]]; 
		
        t := Table[Last[w2D[n]], \ {m}]; 

        	{Sum[Apply[Plus, t[\([w]\)]^2], \ {w, 1, m}]/m, 	\ 
	
          Sum[Sqrt[Plus\ @@\ \ t[\([w]\)]^2], \ {w, 1, m}]/m}]\)

Table[AverageDistance[10,30]//N,{10}]

Mean-Square, Radius of Gyration

locslocs = {{1, 0}, {2, -1},  {2, -2}}n = k

Transpose[locslocs]

cm = N[Apply[Plus, locslocs]/(n + 1)]

(Transpose[locslocs] - cm)^2

Apply[Plus,  Flatten[(Transpose[locslocs] - cm)^2]]

MeanSquareRadiusGyration[n_Integer, m_Integer] := 
  Module[{squareRadiusGyration},
      squareRadiusGyration[s_Integer] := 
         Module[{locs, cm, choices = {{1,0},{-1,0},{0,1},{0,-1}}},
          locs = FoldList[Plus,{0,0}, 
             choices[[Table[ Random[Integer,{1,4}],{s}]]]];
          cm = N[Apply[Plus, locs]/(s+1)];
          Apply[Plus, Flatten[(Transpose[locs] - cm)^2]]/(s+1)
                    ];
      N[Sum[squareRadiusGyration[n], {m}]/m]
              ]

MeanSquareRadiusGyration[10, 50]

The Critical Exponent of a Random Walk

Range[10, 90, 20]

data = ({MeanSquareDistance[#1, 75], #1}&)/@Range[10, 90, 20]

data = Map[{MeanSquareDistance[#, 75], #}&, Range[10, 90, 20]]

data1 = Log[data] // N

calculatedExponent = Fit[N[Log[data]], {x}, x]

calculatedGraph = Plot[calculatedExponent, {x, 0, 90}];

Needs["Graphics`Graphics`"]

data

dataGraph = LogLogListPlot[data,
		PlotStyle -> PointSize[.03] ];

Show[dataGraph, calculatedGraph] ;

Simple Graphics

ShowWalk2D[coords_, opts___]:=
	Show[Graphics[Line[coords],
		opts, AspectRatio -> Automatic]]

units[xmin_, xmax_] := Range[Floor[xmin], Floor[xmax], 1]

Walk2D[50]

ShowWalk2D[%, Ticks->{units,units},Axes->True, AspectRatio->1] ;

ShowWalk2D[ Walk2D[500],
	DefaultColor -> Hue[.5],
	Background -> GrayLevel[0.5],Axes->True, AspectRatio->1 ] ;